import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

const store = new Vuex.Store({
  plugins: [createPersistedState()],
  state: {
    websock: null,
    userInfo: {
      name: 'asdasd',
      age: 24
    },
    worker: null
  },
  mutations: {
    websock_init (state, url) {
      if (typeof WebSocket === 'undefined') {
        console.log('【websocket】没有websocket类')
      } else {
        state.websock = new WebSocket(url)
        // 监听socket连接
        state.websock.onopen = function () {
          console.log('【websocket】连接成功')
        }
        // 监听socket错误信息
        state.websock.onerror = function (error) {
          console.log(`【websocket】发生错误：${error}`)
        }
        // 监听socket消息
        state.websock.onmessage = function (msg) {
          console.log('【websocket】接收消息：', msg.data)
        }
        // 监听socket消息
        state.websock.onclose = function () {
          console.log('【websocket】连接关闭')
        }
      }
    }
  },
  actions: {
  },
  modules: {
  },
  getters: {
  }
})
export default store
